<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action{
	
	public function _initialize(){
		$navs=array(
				array(
					'id'	=> 'index',
					'href'	=> U('Index/index'),
					'title'	=> '首页',
					'npage'	=> false,
					),
		);
		$this->assign("navs",$navs);		
	}
    public function index(){
		if ($_SESSION['slogin'] == 1) $this->redirect('Index/userpage', array(), 0,'未登录！');
		
		session_unset(); 
		session_destroy(); 
		$_SESSION['slogin']=0;
		
		$this->assign("content", "Index:Index");
		$this->display("Public:base");
    }
	public function userpage(){
		if($_SESSION['slogin'] != 1) $this->redirect('Index/index', array(), 0,'未登录！');
		$user = M('User');
		$userinfo = $user->where( "uid='{$_SESSION['uid']}'" )->find();
		if ($userinfo['tel']==NULL ) $this->redirect('Index/step1', array(), 0,'跳~！');
		
		$mscore = M('record');
		$score = $mscore->where( "uid='{$_SESSION['uid']}' AND sms=1" )->select();
		
		$mscore = M('record');
		$score2 = $mscore->where( "uid='{$_SESSION['uid']}' AND sms=0" )->select();
		
		$this->assign("score", $score);
		$this->assign("score2", $score2);
		$this->assign("userinfo", $userinfo);
		$this->assign("content", "Index:userpage");
		$this->display("Public:base");
	}
	public function del(){
		$this->assign("content", "Index:del");
		$this->display("Public:base");
	}
	public function delyes(){
		$user = M('User');
		$userinfo = $user->where( "uid='{$_SESSION['uid']}'" )->find();
		$userinfo['tel']=NULL;
		$user->where( "uid='{$_SESSION['uid']}'" )->save($userinfo);
		$this->redirect('Index/index', array(), 2,'取消成功');
	}
	public function logout(){
		session_unset(); 
		session_destroy(); 
		$_SESSION['slogin']=0;
		$this->redirect('Index/index', array(), 0,'未登录！');
	}
	public function login(){
		if ( $_POST['uid']=='' || $_POST['pw']=='') 
			$this->redirect('Index/index', array(), 0,'登录成功~0秒后自动跳转');
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ';
		
		$output=catcha('http://electsys0.sjtu.edu.cn/edu/index.aspx',0);
		preg_match("|Set-Cookie: ASP.NET_SessionId=(.*); path=/|", $output, $matches);
        
		$cook = array(

			'ASP.NET_SessionId' => $matches[1],
			'JASiteCookie' => '',
		);

		$post_data = array(

		);
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/index.aspx", 0, $post_data, $cook, 0);


		$post_data['txtUserName']= $_POST['uid'];
		$post_data['txtPwd']= $_POST['pw'];
		$post_data['rbtnLst']='1';
		$post_data['Button1']='登录';
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/index.aspx",0, $post_data, $cook, 0);
		$post_data = array(

		);
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/student/sdtinfocheck.aspx",0, $post_data, $cook, 0);
		$result = catcha("http://electsys0.sjtu.edu.cn/edu/student/sdtleft.aspx",0, $post_data, $cook, 0);
		//echo $result;
		preg_match_all('|<span id="lblXm">(.*?)</span>的服务平台|', $result, $matches);
		//print_r($matches);
		if (count($matches[1])>0){
			//login ok!
			$_SESSION['slogin']=1;
			$_SESSION['uid']=$_POST['uid'];
			$_SESSION['pw']=$_POST['pw'];
			$_SESSION['name']=$matches[1][0];
			$user = M('User');
			$userinfo = $user->where( "uid='{$_SESSION['uid']}'" )->find();
			if ($userinfo!=NULL)
			{
				$userinfo['rnum'] = rand(1000,9999);
				$userinfo['pw'] = base64_encode($_SESSION['pw']);
				$userinfo['ip'] = get_client_ip();
				$userinfo['time'] = time();
				$user->where( "uid='{$_SESSION['uid']}'" )->save($userinfo);
				if ($userinfo['tel'] == NULL) $this->redirect('Index/step1', array(), 0,'跳~！');
			}
			else
			{
				$userinfo['uid'] = $_SESSION['uid'];
				$userinfo['pw'] = base64_encode($_SESSION['pw']);
				$userinfo['name'] = $_SESSION['name'];
				$userinfo['rnum'] = rand(1000,9999);
				$userinfo['ip'] = get_client_ip();
				$userinfo['time'] = time();
				$user->add($userinfo);
				$this->redirect('Index/step1', array(), 0,'跳~！');
			}
			$this->redirect('Index/userpage', array(), 0,'跳~！');
		}
		else $this->redirect('Index/index', array(), 2,'用户名或密码错误！');
		$this->assign("content", "Index:login");
		$this->display("Public:base");
	}
	
	public function step1(){
	
		if($_SESSION['slogin'] != 1) $this->redirect('Index/index', array(), 0,'未登录！');
		$this->assign("content", "Index:step1");
		$this->display("Public:base");
	}
	public function stepsms(){
		if($_SESSION['slogin'] != 1) $this->redirect('Index/index', array(), 0,'未登录！');
		preg_match_all('|(1\d{10})|',$_POST['tel'], $matches);
		
		if (count($matches[1])==0) $this->redirect('Index/step1', array(), 2,'手机号码错误！');
		//sms
		$_SESSION['tel']=$_POST['tel'];
		$user = M('User');
		$userinfo = $user->where( "uid='{$_SESSION['uid']}'" )->find();
		//print_r($userinfo);
		$r = catcha('http://www.tui3.com/api/send/?k=b33fc816fae5dec73ba1696b3c50f321&r=xml&p=1&t='.$_POST['tel'].'&c=您好,您的手机验证码为'.$userinfo['rnum'].',by@YEPINFO',0);  
		//echo $r;
		$this->redirect('Index/step2', array(), 0,'未登录！');	
	}
	
	public function step2(){
		if($_SESSION['slogin'] != 1) $this->redirect('Index/index', array(), 0,'未登录！');
		
		$this->assign("content", "Index:step2");
		$this->display("Public:base");
	}
	public function step3(){
		if($_SESSION['slogin'] != 1) $this->redirect('Index/index', array(), 0,'未登录！');
		if ($_POST['rnum']==NULL || $_SESSION['tel']==NULL) 
			$this->redirect('Index/step1', array(), 0,'登录成功~0秒后自动跳转');
		$user = M('User');
		$userinfo = $user->where( "uid='{$_SESSION['uid']}'" )->find();
		if ($_POST['rnum'] != $userinfo['rnum']) $this->redirect('Index/step2', array(), 2,'验证码错误,请重新输入');
		//echo $_SESSION['tel'];
		$userinfo['tel']=$_SESSION['tel'];
		$user->where( "uid='{$_SESSION['uid']}'" )->save($userinfo);
		
		$output=catcha('http://electsys0.sjtu.edu.cn/edu/index.aspx',0);
		preg_match("|Set-Cookie: ASP.NET_SessionId=(.*); path=/|", $output, $matches);
		
		$cook = array(

			'ASP.NET_SessionId' => $matches[1],
			'JASiteCookie' => '',
		);

		$post_data = array(

		);
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/index.aspx", 0, $post_data, $cook, 0);


		$post_data['txtUserName']= $userinfo['uid'];
		$post_data['txtPwd']= base64_decode($userinfo['pw']);
		$post_data['rbtnLst']='1';
		$post_data['Button1']='登录';
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/index.aspx",0, $post_data, $cook, 0);
		$post_data = array(

		);
		//print_r($post_data);
		catcha("http://electsys0.sjtu.edu.cn/edu/student/sdtinfocheck.aspx",0, $post_data, $cook, 0);
		$post_data = array(

		);
		catcha("http://electsys0.sjtu.edu.cn/edu/StudentScore/B_StudentScoreQuery.aspx",0, $post_data, $cook, 0);
		
		$post_data["ddlXN"] = CCXN;
		$post_data["ddlXQ"]	= CCXQ;
		$post_data["txtKCDM"] = "";
		$post_data["btnSearch"]	= "  查  询  ";

		$result = catcha("http://electsys0.sjtu.edu.cn/edu/StudentScore/B_StudentScoreQuery.aspx",0, $post_data, $cook, 0);
		preg_match_all('|<td align="center">(\S*)\s*</td><td width="180">(\S*)\s*</td><td align="center">(\S*)\s*</td><td align="center">(\S*)\s*</td><td align="center">|', $result, $matches);
		//print_r($matches);
		$score=array();
		for ($i=0;$i<count($matches[1]);$i++)
		{
			$score[$i]['cid']=$matches[1][$i];
			$score[$i]['cname']=$matches[2][$i];
			$score[$i]['credit']=$matches[3][$i];
			$score[$i]['score']=$matches[4][$i];
			
			$score[$i]['uid']=$_SESSION['uid'];
			$score[$i]['time']=time();
			
			$score[$i]['CCXN']=CCXN;
			$score[$i]['CCXQ']=CCXQ;
			
			$mscore = M('record');
			$scoreinfo = $mscore->where( "uid='{$_SESSION['uid']}' AND cid='{$score[$i]['cid']}'" )->find();
			$add=$score[$i];
			//print_r($add);
			if ($scoreinfo==NULL) $mscore->add($add);
		}
		//print_r($score);
		$this->assign("score", $score);
		$this->assign("content", "Index:step3");
		$this->display("Public:base");
	
	}
	
	public function test(){
	$output=catcha('http://electsys0.sjtu.edu.cn/edu/index.aspx',0);
	preg_match("|Set-Cookie: ASP.NET_SessionId=(.*); path=/|", $output, $matches);
	print_r( $matches);
	}
}
?>